﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class UserDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        private RoleBLO _roleBLO = new RoleBLO();
        /// <summary>
        /// Get All User Is Supplier
        /// </summary>
        /// <returns></returns>
        public IEnumerable<User> GetAllSupplier()
        {
            IEnumerable<User> listSupplier = new List<User>();
            var resultQuery = from supplier in _entities.Users
                              where supplier.IsActive == true
                                    && supplier.IsDeleted == false
                                    && supplier.RoleId == 4
                              select supplier;
            listSupplier = resultQuery;
            return listSupplier;
        }

        /// <summary>
        /// Check login
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>roleId</returns>
        ///  /// <author>HungPH</author>
        /// <date>3/4/2014</date>
        public int GetRoleIdFromLogin(String username, String password)
        {
            User user = LoadUserByUserName(username);
            int roleId = 0;
            if (user != null)
            {
                if (user.Password == password && user.IsDeleted == false)
                {
                    roleId = user.RoleId;
                    return roleId;
                }
            }
            return roleId;
        }

        /// <summary>
        /// get list of staffs
        /// </summary>
        /// <returns>List of users</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> LoadAllStaffs()
        {
            int roleId = _roleBLO.GetRoleIdByName("Staff");
            var users = from user in _entities.Users where user.IsDeleted == false && user.RoleId == roleId select user;
            return users.ToList();
        }

        /// <summary>
        /// get list of customers
        /// </summary>
        /// <returns>List of customers</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> LoadAllCustomers()
        {
            int roleId = _roleBLO.GetRoleIdByName("Customer");
            var users = from user in _entities.Users where user.IsDeleted == false && user.RoleId == roleId select user;
            return users.ToList();
        }

        /// <summary>
        /// get list of suppliers
        /// </summary>
        /// <returns>List of users</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> LoadAllSuppliers()
        {
            int roleId = _roleBLO.GetRoleIdByName("Supplier");
            var users = from user in _entities.Users where user.IsDeleted == false && user.RoleId == roleId select user;
            return users.ToList();
        }

        /// <summary>
        /// get a User by UserName
        /// </summary>
        /// <returns>User</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public User LoadUserByUserName(String username)
        {
            var user = _entities.Users.Find(username);
            return _entities.Users.Find(username);
        }

        /// <summary>
        /// add staff
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int AddStaff(User staff)
        {
            try
            {
                staff.RoleId = _roleBLO.GetRoleIdByName("Staff");
                staff.IsDeleted = false;
                staff.IsActive = true;
                _entities.Users.Add(staff);
                _entities.SaveChanges();
                return _entities.SaveChanges();
            }
            catch { return 0; }
        }

        /// <summary>
        /// register
        /// </summary>
        /// <param name="customer"></param>
        /// <returns>1 : successful; 2:fail</returns>
        /// <author>hungph</author>
        public int Register(User customer)
        {
            try
            {
                customer.RoleId = _roleBLO.GetRoleIdByName("Customer");
                customer.IsDeleted = false;
                customer.IsActive = true;
                customer.Point = 0;
                customer.ImageURL = string.Empty;
                _entities.Users.Add(customer);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch { return 0; }
        }

        /// <summary>
        /// add supplier
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int AddSupplier(User supplier)
        {
            try
            {
                supplier.RoleId = _roleBLO.GetRoleIdByName("Supplier");
                supplier.Gender = "";
                supplier.IsActive = true;
                _entities.Users.Add(supplier);
                var resultAdd = _entities.SaveChanges();
                return resultAdd;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// update info of staff
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int EditStaff(User user)
        {
            try
            {
                User tmp = _entities.Users.Find(user.UserName);
                tmp.RoleId = _roleBLO.GetRoleIdByName("Staff");
                tmp.FullName = user.FullName;
                tmp.Password = user.Password;
                tmp.Address = user.Address;
                tmp.PhoneNumber = user.PhoneNumber;
                tmp.Age = user.Age;
                tmp.Email = user.Email;
                tmp.Gender = user.Gender;
                tmp.ImageURL = user.ImageURL;
                _entities.Entry(tmp).State = EntityState.Modified;
                int result = _entities.SaveChanges();  //DuyHDV edited
                return result;
            }
            catch { return 0; }
        }

        /// <summary>
        /// update info of customer
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int EditCustomer(User user)
        {
            try
            {
                User tmp = _entities.Users.Find(user.UserName);
                tmp.RoleId = _roleBLO.GetRoleIdByName("Customer");
                tmp.FullName = user.FullName;
                tmp.Gender = user.Gender;
                tmp.Age = user.Age;
                tmp.Address = user.Address;
                tmp.Email = user.Email;
                tmp.PhoneNumber = user.PhoneNumber;
                _entities.Entry(tmp).State = EntityState.Modified;
                int resultQurety = _entities.SaveChanges();
                return resultQurety;
            }
            catch { return 0; }
        }

        /// <summary>
        /// update info of supplier
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int EditSupplier(User user)
        {
            try
            {
                User tmp = _entities.Users.Find(user.UserName);
                tmp.FullName = user.FullName;
                tmp.RoleId = _roleBLO.GetRoleIdByName("Supplier");
                tmp.Password = user.Password;
                tmp.Address = user.Address;
                tmp.PhoneNumber = user.PhoneNumber;
                tmp.Email = user.Email;
                tmp.ImageURL = user.ImageURL;
                _entities.Entry(tmp).State = EntityState.Modified;
                int result = _entities.SaveChanges(); //duyHDV edited 27/03/2014
                return result;
            }
            catch { return 0; }
        }

        /// <summary>
        /// delete user
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public int Delete(User user)
        {
            try
            {
                user.IsDeleted = true;
                _entities.Entry(user).State = EntityState.Modified;
                _entities.SaveChanges();
                return _entities.SaveChanges();
            }
            catch { return 0; }
        }
        /// <summary>
        /// check if
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool IsUsed(User user)
        {
            var querry = _entities.Theaters.Any(s => s.UserName == user.UserName);
            if (querry == true) { return true; }
            else { return false; }
        }
        /// <summary>
        /// active user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int ActiveUser(User user)
        {
            try
            {
                user.IsActive = true;
                _entities.Entry(user).State = EntityState.Modified;
                _entities.SaveChanges();
                return _entities.SaveChanges();
            }
            catch { return 0; }
        }

        /// <summary>
        /// deactive user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int DeactiveUser(User user)
        {
            try
            {
                user.IsActive = false;
                _entities.Entry(user).State = EntityState.Modified;
                _entities.SaveChanges();
                return _entities.SaveChanges();
            }
            catch { return 0; }
        }

        /// <summary>
        /// Check if user name is existed or not
        /// </summary>
        /// <param name="user"></param>
        /// <returns>true || false</returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public bool CheckIsExisted(User user)
        {
            var isExist = _entities.Users.Any(c => c.UserName == user.UserName);
            return isExist;
        }

        /// <summary>
        /// Check if email is existed or not
        /// </summary>
        /// <param name="user"></param>
        /// <returns>true || false</returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public bool CheckEmail(User user)
        {
            var email = user.Email;
            var isExist = _entities.Users.Any(c => c.Email == email);
            return isExist;
        }

        //public int GetUserIdByUserName(string userName)
        //{
        //    var result = (from user in entity.Users
        //                 where user.UserName == userName && user.IsActive == true && user.IsDeleted == false
        //                 select user).FirstOrDefault();

        //    return result.UserId;
        //}

        /// <summary>
        /// change password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>result of change password: 1 success;2 fail</returns>
        /// <author>hungph</author>
        public int ChangePassword(string username, string password)
        {
            try
            {
                User tmp = _entities.Users.Find(username);
                tmp.Password = password;
                _entities.Entry(tmp).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch { return 0; }
        }

        /// <summary>
        /// Get status of username
        /// </summary>
        /// <param name="username"></param>
        /// <returns>status of username</returns>
        /// <author>hungph</author>
        public bool CheckStatusOfUser(string username)
        {
            try
            {
                User tmp = _entities.Users.Find(username);
                return tmp.IsActive;
            }
            catch { return false; }
        }
    }
}